package com.lagou.module01.servlet;

import com.lagou.module01.bean.Student;
import com.lagou.module01.service.imp.StudentServiceImp;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: xyk
 * @Date: Create in 0:46 2021/3/20
 * @Description:
 */
@WebServlet(name = "QueryStudentServlet", urlPatterns = "/queryStudentServlet")
public class QueryStudentServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Student> students = new ArrayList<>();
        response.setContentType("text/html;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        request.setCharacterEncoding("utf-8");

        PrintWriter out = response.getWriter();

        // service层获取学员信息
        StudentServiceImp studentServiceImp = new StudentServiceImp();

        String id = request.getParameter("key");

        try {
            Student student = studentServiceImp.queryStudentsService(id);
            students.add(student);
        }
        catch (Exception e){
            out.print(e);
        }
        // 将总页数和所有学员列表放入内置对象中，放入session中
        request.getSession().setAttribute("studentsList", students);
        request.getSession().setAttribute("totalPages", 1);
        request.getSession().setAttribute("currentPage", "1");

        // 重定向到学员管理界面
        response.sendRedirect("studentManage.jsp");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
